package com.alasbusy.alasbusyapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.bean.hometool.HomeToolBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/10.
 */

public class HomeToolAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<HomeToolBean> mData = new ArrayList<>();
    public static final int LOADING_NO = 2;

    private static final int TYPE_ITEM = 1;  //普通Item View

    private LayoutInflater mInflater;

    public HomeToolAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
    }

    public void setList(List<HomeToolBean> list) {
        mData.clear();
        mData.addAll(list);
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemTwoViewHolder viewHolder = new ItemTwoViewHolder(mInflater.inflate(R.layout.home_tool_item, parent, false));
            return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
      if(holder instanceof ItemTwoViewHolder){
            ItemTwoViewHolder itemTwoViewHolder = (ItemTwoViewHolder) holder;
          HomeToolBean homeToolBean = mData.get(position);
          itemTwoViewHolder.setData(homeToolBean, position);
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    /**
     * 进行判断是普通Item视图还是FootView视图
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
            return TYPE_ITEM;
    }

    public class ItemTwoViewHolder extends RecyclerView.ViewHolder {

        ImageView tool_item_img;
        private final TextView tool_item_title;


        public ItemTwoViewHolder(View itemView) {
            super(itemView);
            tool_item_img = itemView.findViewById(R.id.tool_item_img);
            tool_item_title = itemView.findViewById(R.id.tool_item_title);
        }

        public void setData(HomeToolBean data, int position) {
            tool_item_img.setImageResource(data.getImg());
            tool_item_title.setText(data.getTxt());

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
        }
    }
}
